﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Acando.Dynamics.CRM.Silverlight.Library.MetaSdk;
using System.Xml;
using Acando.Dynamics.CRM.Silverlight.Library.Helper;
using Acando.Dynamics.CRM.Silverlight.Library.Controls;
using Acando.Dynamics.CRM.Silverlight.Library.Types;
using System.Xml.Linq;
using System.ComponentModel;
using Acando.Dynamics.CRM.Silverlight.Library.CrmSdk;
using Acando.Dynamics.CRM.Silverlight.Library.Interfaces;
using Acando.Dynamics.CRM.Silverlight.Library.Extensions;

namespace RiaCrm.EditRecord
{
    public partial class EditRecordView : UserControl, IPage
    {
        private EditRecordViewModel _viewModel;
        
        /* Constructor */
        public EditRecordView(EditRecordViewModel viewModel)
        {
            InitializeComponent();

            this.DataContext = viewModel;
            this.tabAreas.DataContext = viewModel.CurrentItem as DynamicEntity;
            this._viewModel = viewModel;
            this._viewModel.PropertyChanged += new PropertyChangedEventHandler(_viewModel_PropertyChanged);
               
        }

        private void _viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CurrentItem")
            {
                this.LoadControls(this._viewModel.FormDefinition);
                this.tabAreas.DataContext = this._viewModel.CurrentItem as DynamicEntity;
            }
            if (e.PropertyName == "FormDefinition")
            {
                this.LoadControls(this._viewModel.FormDefinition);
            }
        }

        private void LoadControls(Pair<EntityMetadata, XElement> fromDefinition)
        {
            //if (fromDefinition == null)
            if (fromDefinition.IsNull())
                return;

            this.pnlLoading.Visibility = Visibility.Visible;

            
            this.tabAreas.Items.Clear();
            this._viewModel.RegisteredPicklists.Clear();

            //Iterate over all tabs of main form
            foreach (var tab in fromDefinition.Type2.Elements("tabs").Elements("tab"))
            {
                TabItem tabItem = new TabItem();
                tabItem.Header = (from lbl in tab.Elements("labels").Elements("label")
                                  where lbl.Attribute("languagecode").Value == ApplicationContext.Current.GetUserLanguage().ToString()
                                  select lbl.Attribute("description").Value).First();

                StackPanel stackPanel = new StackPanel();

                foreach (var section in tab.Elements("sections").Elements("section"))
                {
                    stackPanel.Children.Add(new SectionControl(this._viewModel, fromDefinition.Type1, section));
                }
                tabItem.Content = stackPanel;

                this.tabAreas.Items.Add(tabItem);
            }

            this.pnlLoading.Visibility = Visibility.Collapsed;

        }

        #region IPage Members

        public Panel ParentElementForDialogs
        {
            get { return this.formGrid; }
        }

        #endregion
    }
}
